package emulator.GUI.io;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import emulator.util.AddressRange;

import layout.SpringUtilities;

@SuppressWarnings("serial")
public class ShiftPanel extends JPanel
{
	private JTextField latchEdit = new JTextField("%MMMMMMMM");
	private JTextField shiftEdit = new JTextField("%MMMMMMMM");
	private JTextField modeEdit = new JTextField("0");
	private String[] modeText;

	public ShiftPanel(String title, String[] modes)
	{
		super();
		modeText = modes;
		initPanel(title);
	}

	private void initPanel(String title)
	{
		setLayout(new SpringLayout());
		setBorder(BorderFactory.createTitledBorder(title));
		
		add(new JLabel("Latch"));
		add(latchEdit);
		add(new JLabel("Value"));
		add(shiftEdit);
		add(new JLabel("Mode"));
		add(modeEdit);
		
		SpringUtilities.makeGrid(this,
                3, 2, //rows, cols
                5, 5, //initialX, initialY
                5, 5);//xPad, yPad
	}

	public void setShiftValue(int shiftValue)
	{
		shiftEdit.setText("%"+AddressRange.toBinaryString(shiftValue, 8));
	}

	public int getShiftValue()
	{
		return AddressRange.addressFromString(shiftEdit.getText());
	}

	public void setLatchValue(int latchValue)
	{
		latchEdit.setText("%"+AddressRange.toBinaryString(latchValue, 8));
	}
	
	public int getLatchValue()
	{
		return AddressRange.addressFromString(latchEdit.getText());
	}
	
	public void setModeValue(int modeValue)
	{
		modeEdit.setText(""+modeValue);
		if (modeValue >= 0 && modeValue < modeText.length)
			modeEdit.setToolTipText(modeText[modeValue]);
		else
			modeEdit.setToolTipText("???");
	}
	
	public int getModeValue()
	{
		return Integer.parseInt(modeEdit.getText());
	}
}
